<?php

namespace app\admin\model;

use fast\Random;

class Auth
{
    protected $expires_in = 3 * 3600;
    protected $refresh_token_expires_in = 30 * 86400;

    /**
     * 生成token
     */
    protected function generateToken($prefix = '')
    {
        for ($i = 0; $i < 100; $i++) {
            $token = $prefix . Random::uuid();
            if (null === cache($token)) {
                return $token;
            }
        }
        return false;
    }

    /**
     * 获取token
     */
    public function getToken($admin_id)
    {
        $access_token  = $this->generateToken('a');
        $refresh_token = $this->generateToken('ar');
        if (empty($access_token) || empty($refresh_token)) {
            return false;
        }

        $admin_token_key = 'admin_token_' . $admin_id;

        $data = [
            'admin_id' => $admin_id,
            'access_token' => $access_token,
            'refresh_token' => $refresh_token,
        ];
        cache($access_token, $data, $this->expires_in);
        cache($refresh_token, $data, $this->refresh_token_expires_in);
        cache($admin_token_key, $data, $this->refresh_token_expires_in);

        return [
            'access_token' => $access_token,
            'refresh_token' => $refresh_token,
            'expires_in' => $this->expires_in,
        ];
    }

    /**
     * 刷新token
     */
    public function refreshToken(string $refresh_token)
    {
        $data = cache($refresh_token);
        if (empty($data)) {
            return false;
        }

        if (false === cache($data['access_token'])) {
            // access token 过期，重新生成
            $access_token = $this->generateToken('a');
            if (empty($access_token)) {
                return false;
            }
        } else {
            // access token 还未过期，保持不变
            $access_token = $data['access_token'];
        }

        $admin_token_key = 'admin_token_' . $data['admin_id'];

        $data['access_token'] = $access_token;
        cache($access_token, $data, $this->expires_in);
        cache($refresh_token, $data, $this->refresh_token_expires_in);
        cache($admin_token_key, $data, $this->refresh_token_expires_in);

        return [
            'access_token' => $access_token,
            'refresh_token' => $refresh_token,
            'expires_in' => $this->expires_in,
        ];
    }

    /**
     * 验证token
     */
    public function verifyToken(string $token)
    {
        $data = cache($token);
        if (empty($data)) {
            return false;
        }
        return $data;
    }

    /**
     * 删除token
     */
    public function deleteToken($access_token)
    {
        if (empty($access_token)) {
            return true;
        }
        $data = cache($access_token);
        if (empty($data)) {
            return true;
        }

        $admin_token_key = 'admin_token_' . ($data['admin_id'] ?? 0);

        cache($data['access_token'], null);
        cache($data['refresh_token'], null);
        cache($admin_token_key, null);

        return true;
    }
}
